package com.todod.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.todod.entity.ResultData;
import com.todod.mapper.CatalogDetailMapper;
import com.todod.mapper.CatalogManageMapper;
import com.todod.mapper.ProjectFileMapper;
import com.todod.mapper.ProjectInfoMapper;
import com.todod.model.CatalogDetail;
import com.todod.model.CatalogManage;
import com.todod.model.ProjectFile;
import com.todod.model.ProjectInfo;
import com.todod.model.User;
import com.todod.service.ICatalogDetailService;
import com.todod.service.IUserService;

/**
 * @author dyl
 * @date 2022-09-05
 */
@RestController
@RequestMapping("/catalogDetail")
public class CatalogDetailController extends BaseManageController {

	@Autowired
	public ICatalogDetailService catalogDetailService;
	@Autowired
	public CatalogDetailMapper catalogDetailMapper;
	@Autowired
	private IUserService iUserService;
	@Autowired
	public ProjectInfoMapper projectInfoMapper;
	@Autowired
	public CatalogManageMapper catalogManageMapper;
	@Autowired
	private ProjectFileMapper projectFileMapper;

	/**
	 * 
	 * @param _userId
	 * @return
	 */
	@RequestMapping("/getTreeListInAuthority")
	public ResultData getTreeListInAuthority(@RequestAttribute Long _userId, Long classifyId) {
		// 获取权限数据
		List<CatalogDetail> list = catalogDetailService.getTreeListInAuthority(classifyId);
		return ResultData.success("ok", list);
	}

	/**
	 * 
	 * @param _userId
	 * @return
	 */
	@RequestMapping("/getInitialLoading")
	public ResultData getInitialLoading(@RequestAttribute Long _userId, Long classifyId) {
		// 获取权限数据
		List<CatalogDetail> list = catalogDetailService.getListByUserIdAndType(_userId, classifyId);
		for (CatalogDetail catalogDetail : list) {
			User user = iUserService.getById(catalogDetail.getCreateuserId());
			catalogDetail.setCreateuserName(user.getNickName());
		}
		return ResultData.success("ok", list);
	}

	/**
	 * 
	 * @param _userId
	 * @return
	 */
	@RequestMapping("/getAgainLoading")
	public ResultData getAgainLoading(@RequestAttribute Long _userId, Long parentId, String typeName) {
		// 获取权限数据
		List<CatalogDetail> list = catalogDetailService.getListByUserIdAndTypeAndParentId(_userId, parentId, typeName);
		for (CatalogDetail catalogDetail : list) {
			User user = iUserService.getById(catalogDetail.getCreateuserId());
			catalogDetail.setCreateuserName(user.getNickName());
		}
		return ResultData.success("ok", list);
	}

	/**
	 * 
	 * @param _userId
	 * @param reqCatalogDetail
	 * @return
	 */
	@PostMapping("/save")
	public ResultData saveData(@RequestAttribute Long _userId, @RequestBody CatalogDetail catalogDetail) {

		boolean flag = true;
		if (catalogDetail.getParentId() != 1l) {
			CatalogDetail catalogDetail1 = catalogDetailMapper.selectById(catalogDetail.getParentId());
			if (catalogDetail1.getLevels() != null && StringUtils.isNotBlank(catalogDetail1.getLevels())) {
				String[] levelP = catalogDetail1.getLevels().split(";");
				if (levelP.length == 1) {
					flag = true;
				} else {
					flag = false;
				}
			} else {
				flag = true;
			}
		}

		if (flag == true) {
			if (catalogDetail.getId() != null) {
				catalogDetailService.editData(_userId, catalogDetail);
				return ResultData.success();
			} else {
				catalogDetailService.saveData(_userId, catalogDetail);
				return ResultData.success();
			}
		} else {
			return ResultData.error("最多只可以创建三级目录,请修改后再进行保存");
		}
	}

	/**
	 * 
	 * @param _userId
	 * @param CatalogDetail
	 * @return
	 */
	@PostMapping("/copy")
	public ResultData copyData(@RequestAttribute Long _userId, @RequestBody CatalogDetail catalogDetail) {
		catalogDetailService.copyData(_userId, catalogDetail);
		return ResultData.success();
	}

	/**
	 * 
	 * @param _userId
	 * @param CatalogDetail
	 * @return
	 */
	@PostMapping("/move")
	public ResultData moveData(@RequestAttribute Long _userId, @RequestBody CatalogDetail catalogDetail) {
		catalogDetailService.moveData(_userId, catalogDetail);
		return ResultData.success();
	}

	/**
	 * @title deleteInLogic
	 * @description 逻辑删除
	 * @param _userId
	 * @param ids
	 * @param status(-1:删除 ;1:使用)
	 * @return
	 */
	@PostMapping("/deleteInLogic")
	public ResultData deleteInLogic(@RequestAttribute Long _userId, String ids, String status) {
		String[] split = ids.split(",");
		for (String s : split) {
			Long id = Long.valueOf(s);
			CatalogDetail catalogDetail = catalogDetailMapper.selectById(id);
			if (catalogDetail != null) {
				Map<String, Object> map = catalogDetailService.getChildren(catalogDetail.getId());
				if (map != null && map.size() > 0) {
					return ResultData.error("当前目录下存在子目录或文件，请先删除子文件！");
				} else {
					catalogDetailMapper.deleteById(id);
//					catalogDetailService.deleteInLogic(_userId, id, status);
				}
			}
		}

		return ResultData.success();
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	@GetMapping("/getInfo")
	public ResultData getInfo(Long code) {
		if (code == null) {
			return ResultData.error(ResultData.PARAM_ERROR_CODE, "参数错误");
		}
		return ResultData.success("ok", catalogDetailService.getInfo(code));
	}

	/**
	 * 
	 * @param _userId
	 * @return
	 */
	@RequestMapping("/getAllLeafNode")
	public ResultData getAllLeafNode(@RequestAttribute Long _userId, Long xiangmuId) {

		ProjectInfo projectInfo = projectInfoMapper.selectById(xiangmuId);
		// 获取权限数据
		List<CatalogDetail> list = catalogDetailService.getTreeListInAuthority(projectInfo.getCatalogId());
		return ResultData.success("ok", list);
	}

	/**
	 * 
	 * @param companyId
	 * @param _userId
	 * @param request
	 * @param queryProjectFile
	 * @return
	 */
	@RequestMapping("/getInitialLoading2")
	public ResultData getInitialLoading2(@RequestAttribute Long _userId) {
		QueryWrapper<CatalogManage> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("cm_code", "PUBLIC_CATALOG");
		queryWrapper.eq("cm_state", "1");

		CatalogManage catalogManage = catalogManageMapper.selectOne(queryWrapper);

		List<CatalogDetail> list = null;
		if (catalogManage != null) {
			list = catalogDetailService.getListByUserIdAndType(_userId, catalogManage.getId());
			for (CatalogDetail catalogDetail : list) {
				User user = iUserService.getById(catalogDetail.getCreateuserId());
				catalogDetail.setCreateuserName(user.getNickName());
			}
		}
		return ResultData.success("ok", list);
	}

	/**
	 * 
	 * @param _userId
	 * @return
	 */
	@RequestMapping("/getAllLeafNode2")
	public ResultData getAllLeafNode2(@RequestAttribute Long _userId, Long code) {

		CatalogDetail catalogDetail = catalogDetailMapper.selectById(code);
		// 获取权限数据
		return ResultData.success("ok", catalogDetail);
	}

	/**
	 * 
	 * @param _userId
	 * @return
	 */
	@RequestMapping("/getInitialLoading1")
	public ResultData getInitialLoading1(@RequestAttribute Long _userId, Long xiangmuId) {

		ProjectInfo projectInfo = projectInfoMapper.selectById(xiangmuId);

		// 获取权限数据
		List<CatalogDetail> list = catalogDetailService.getListByUserIdAndType(_userId, projectInfo.getCatalogId());
		for (CatalogDetail catalogDetail : list) {
//			User user = iUserService.getById(catalogDetail.getCreateuserId());
//			catalogDetail.setCreateuserName(user.getNickName());

			List<ProjectFile> pageChildren = projectFileMapper.getListByParentIdAndXftId(projectInfo.getId(),
					catalogDetail.getId());
			if (pageChildren != null && pageChildren.size() > 0) {
				catalogDetail.setTypeName(catalogDetail.getTypeName() + "(" + pageChildren.size() + ")");
			} else {
				catalogDetail.setTypeName(catalogDetail.getTypeName() + "(0)");
			}
		}
		return ResultData.success("ok", list);
	}

	/**
	 * 
	 * @param _userId
	 * @return
	 */
	@RequestMapping("/getAgainLoading1")
	public ResultData getAgainLoading1(@RequestAttribute Long _userId, Long parentId, Long xiangmuId) {
		// 获取权限数据
		List<CatalogDetail> list = catalogDetailService.getListByUserIdAndTypeAndParentId(_userId, parentId, null);
		for (CatalogDetail catalogDetail : list) {
			User user = iUserService.getById(catalogDetail.getCreateuserId());
			catalogDetail.setCreateuserName(user.getNickName());

			List<ProjectFile> pageChildren = projectFileMapper.getListByParentIdAndXftId(xiangmuId,
					catalogDetail.getId());
			if (pageChildren != null && pageChildren.size() > 0) {
				catalogDetail.setTypeName(catalogDetail.getTypeName() + "(" + pageChildren.size() + ")");
			} else {
				catalogDetail.setTypeName(catalogDetail.getTypeName() + "(0)");
			}
		}
		return ResultData.success("ok", list);
	}

}
